cc.Class({
    extends: cc.Component,

    properties: {
        shop_bet: cc.Node,
        content: cc.Node,
    },

    onEnable() {
        this.lobbyMain = cc.find("Canvas").getComponent("lobbyMain");
        this.lobbyNet = this.lobbyMain.lobbyNet;
        this.playerInfo = this.lobbyMain.playerInfo;
        this.lobbyMain.openLoading();

        this.getBetRecord(); //从服务器获取记录
    },

    start() {

    },

    onClick(event, target) {
        this.lobbyMain.playClickSound();
        if (target == "close") {
            this.closePanel();
        }
    },

    // 从服务器获取记录
    getBetRecord() {
        let data = {
            action: "getBetRecord",
            playerId: this.playerInfo.playerId,
        }
        this.lobbyNet.sendMessage(data);
    },


    // 获取记录返回
    onGetBetRecord(data) {
        this.lobbyMain.loading.active = false;

        if (data.respCode == "SUCCESS") {
            let result = data.data;
            result.sort((a, b) => new Date(b.time) - new Date(a.time));
            for (let i = 0; i < result.length; i++) {
                let elem = result[i];
                let item = cc.instantiate(this.shop_bet);
                let time = this.formatWithLineBreak(elem.time);
                item.getChildByName("time").getComponent(cc.Label).string = time;
                let total = 0;
                let jindu = elem.jindu;
                elem.amount = parseFloat(elem.amount);
                if (elem.isFree) {
                    item.getChildByName("type").getComponent(cc.Label).string = "Activity claim";
                    item.getChildByName("multiple").getComponent(cc.Label).string = "10";
                    total = elem.target * 1
                } else {
                    item.getChildByName("type").getComponent(cc.Label).string = "Recharge";
                    item.getChildByName("multiple").getComponent(cc.Label).string = "3";
                    total = elem.target * 1
                }
                total = total.toFixed(2);
                jindu = parseFloat(jindu);
                jindu = jindu.toFixed(2);
                item.getChildByName("amount").getComponent(cc.Label).string = elem.amount;
                let fenshu = item.getChildByName("fenshu");
                fenshu.getChildByName("label").getComponent(cc.Label).string = (jindu / this.playerInfo.ratio).toFixed(2) + '/' + (total / this.playerInfo.ratio).toFixed(2);
                fenshu.getChildByName("task_pro2").getComponent(cc.ProgressBar).progress = jindu / total;
                if (elem.success) {
                    item.getChildByName("state").getChildByName("dui").active = true;
                    item.getChildByName("state").getChildByName("cuo").active = false;
                } else {
                    item.getChildByName("state").getChildByName("dui").active = false;
                    item.getChildByName("state").getChildByName("cuo").active = true;
                }
                this.content.addChild(item);
            }
        } else {
            this.lobbyMain.showTips("Failed to retrieve bet audit!\nPlease try again.")
        }

    },

    // 格式化日期
    formatWithLineBreak(isoString) {
        return isoString.replace("T", "\n").replace("Z", "").slice(0, 19);
    },

    closePanel() {
        let callback = () => {
            this.content.removeAllChildren();
        }
        this.lobbyMain.hidePanel(this.node, callback);
    },

    // update (dt) {},
});
